<?php namespace Summer\Location\Behaviors;

use Db;
use Summer\Location\Models\AreaModel;
use Summer\Location\Models\CityModel;
use Summer\Location\Models\ProvinceModel;
use Summer\Location\Models\TownModel;
use System\Classes\ModelBehavior;
use ApplicationException;
use Exception;

/**
 * Location model extension
 *
 * Adds Country and State relations to a model
 *
 * Usage:
 *
 * In the model class definition:
 *
 *   public $implement = ['RainLab.Location.Behaviors.LocationModel'];
 *
 */
class LocationModel extends ModelBehavior
{
    /**
     * Constructor
     */
    public function __construct($model)
    {
        parent::__construct($model);

        $model->belongsTo['province'] = ['Summer\Location\Models\ProvinceModel','key'=>'province_code','otherKey'=>'code'];
        $model->belongsTo['city']   = ['Summer\Location\Models\CityModel','key'=>'city_code','otherKey'=>'code'];
        $model->belongsTo['area']   = ['Summer\Location\Models\AreaModel','key'=>'area_code','otherKey'=>'code'];
        $model->belongsTo['town']   = ['Summer\Location\Models\TownModel','key'=>'town_code','otherKey'=>'code'];
    }

    public function getProvinceOptions()
    {
        return ProvinceModel::getNameList();
    }

    public function getCityOptions()
    {
        return CityModel::getNameList($this->model->province);
    }
    public function getAreaOptions()
    {
        return AreaModel::getNameList($this->model->city);
    }
    public function getTownOptions()
    {
        return TownModel::getNameList($this->model->area);
    }


}
